#!/bin/sh
#set -n
#et -x
gdoservice=1
gcopycurr=tee
gverbose=log4j.properties
gdoagent=1
gdosynonymonly=0
gwaitjava=0
gvdir=k2-5.5.0u
gsuffix=
gextra="-DiRoot= -DiSuffix="
function usage
{
  echo "Usage: vagt.ksh ( -v optionally do only verity agent tests ) |
          ( -q quiet ) |
          ( -s optionally do only service tests ) " >&2
  exit 1
}
while (( $# > 0 )) ; do
case $1 in
(-v)
  gdoservice=0
  gsuffix="v"
  shift 1
  ;;
(-w)
  gwaitjava=1
  #Need to use cat or ps will not be able to access terminal as tee will also
  gcopycurr="cat >>"
  shift 1
  ;;
(-q2)
  gcopycurr="cat >>"
  shift 1
  ;;
(-s)
  gdoagent=0
  gsuffix="s"
  shift 1
  ;;
(-q)
  gverbose=quietlog4j.properties
  shift 1
  ;;
(-y)
  gdoservice=0
  gdoagent=0
  gdosynonymonly=1
  gsuffix="y"
  shift 1
  ;;
(-k)
  gvdir=k2-5.5.0$2
  shift 2
  ;;
(-l)
  gextra=
  shift 1
  ;;
(-c)
  gclass=$2
  shift 2
(*)
  usage $*
  break
  ;;
esac
done
rm -f $ii/whymel$gsuffix
if (( gdoservice == 1 )) ; then
  serviceparm="-name 'Service?Test.java' -o -name 'Service??Test.java'"
fi
if (( gdoagent == 1 )) ; then
  agentparm="-name 'VerityAgent*Test.java'"
fi
if (( gdosynonymonly == 1 )) ; then
  agentparm="-name 'VerityAgentsy*Test.java'"
fi
if (( (gdoservice == 1) && ( gdoagent == 1) )) ; then
  joinparm="-o"
fi
cd $ii/seeker_cs
eval "find $ii/r22_sdl/src/iutest \( $serviceparm $joinparm $agentparm \)" | \
while read fn ; do
  bfn=$(basename $fn)
  echo "Start $(date) Testing $bfn" | tee -a $ii/whymel$gsuffix
  rm -f $ii/whymel${gsuffix}curr
  eval "ant iutest.selected -Dlog4j.properties=$gverbose $gextra  \
     -Dtarget=target -Dverity_dir_vers=$gvdir -Dselected.files=$bfn 2>&1 | \
     $gcopycurr $ii/whymel${gsuffix}curr" &
  teepid=$!
  if (( gwaitjava == 0 )) ; then
    wait $!
  else
    while (( 1 == 1 )) ; do
      if [[ -f $ii/whymel${gsuffix}curr ]] ; then
        grep '^BUILD' $ii/whymel${gsuffix}curr
        if (( $? == 0 )) ; then
          break
        fi
      fi
      sleep 10
    done
    has_java=0
    export apid=
    ps -fxp$teepid | grep -v PPID | \
      while read auser apid appid arest ; do
        echo "Parent $appid Child $apid"
        ps -fxp$apid
        ahasjava=${arest%%java*}
        if [[ "$arest" != "$ahasjava" ]] ; then
          has_java=1
          echo $apid > $ii/whyme${gsuffix}pid
          break
        fi
        ps -efx | grep " $(whoami) [0-9][0-9]* $apid " | \
        while read auser apid appid arest ; do
          echo "Parent $appid Child $apid"
          ps -fxp$apid
          ahasjava=${arest%%java*}
          if [[ "$arest" != "$ahasjava" ]] ; then
            has_java=1
            echo $apid > $ii/whyme${gsuffix}pid
            break
          fi
          ps -efx | grep " $(whoami) [0-9][0-9]* $apid " | \
          while read auser apid appid arest ; do
            echo "Parent $appid Child $apid"
            ps -fxp$apid
            ahasjava=${arest%%java*}
            if [[ "$arest" != "$ahasjava" ]] ; then
              has_java=1
              echo $apid > $ii/whyme${gsuffix}pid
              break
            fi
          done
        done
      done
      if (( has_java == 1 )) ; then
        ps -fxp$(<$ii/whyme${gsuffix}pid)
        while (( $? == 0 )) ; do
          kill -9 $(<$ii/whyme${gsuffix}pid)
          kill -6 $(<$ii/whyme${gsuffix}pid)
          sleep 5
          ps -fxp$(<$ii/whyme${gsuffix}pid)
        done
        ps -fxp$(<$ii/whyme${gsuffix}pid)
      fi
  fi
  cat $ii/whymel${gsuffix}curr >> $ii/whymel${gsuffix}
done
